package cn.xinfei.xdecision.shutdown;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class GracefulShutdown {

    @Bean
    public WebServerFactoryCustomizer tomcatCustomizer(@Autowired MyShutdown myShutdown) {
        return (WebServerFactoryCustomizer<ConfigurableWebServerFactory>) container -> {
            if (container instanceof TomcatServletWebServerFactory) {
                ((TomcatServletWebServerFactory) container).addConnectorCustomizers(myShutdown);
            }
        };
    }
}
